<template>
    <div class="app-container">
  
      <div class="search-div">
        <el-form label-width="70px" size="small">
          <el-row>
            <el-col :span="4">
                <el-select v-model="searchObj.quType" placeholder="全部类型">
                  <el-option
                    v-for="item in QuTypeOption"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
            </el-col>
            <el-col :span="4">
              <el-input style="width: 95%" v-model="searchObj.content" placeholder="关键字"></el-input>
            </el-col>
            <el-col :span="8">
              <el-form-item label="操作时间">
                <el-date-picker
                  v-model="createTimes"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  style="margin-right: 10px;width: 100%;"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="display:flex">
            <el-button type="primary" icon="el-icon-search" size="mini"  @click="fetchData()">搜索题目</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
          </el-row>
        </el-form>
      </div>
  
      <!-- 工具条 -->
      <div class="tools-div">
        <el-button type="success" icon="el-icon-plus" :disabled="$hasBP('bnt.assignment.add')  === false" size="mini" @click="add">添 加</el-button>
      </div>
  
      <!-- 列表 -->
      <el-table
        v-loading="listLoading"
        :data="list"
        stripe
        border
        style="width: 100%;margin-top: 10px;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column prop="ChineseTypeQu" width="80" label="题目类型"/>
        <el-table-column prop="content" label="题目" show-overflow-tooltip/>
        <el-table-column prop="optionsA" label="选项A" show-overflow-tooltip/>
        <el-table-column prop="optionsB" label="选项B" show-overflow-tooltip/>
        <el-table-column prop="optionsC" label="选项C" show-overflow-tooltip/>
        <el-table-column prop="optionsD" label="选项D" show-overflow-tooltip/>
        <el-table-column prop="answer" label="正确答案" show-overflow-tooltip/>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作"  align="center" fixed="right" width="80">
          <template slot-scope="scope">
            <el-button type="danger" :disabled="$hasBP('bnt.assignment.remove')  === false" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除" />
          </template>
        </el-table-column>
      </el-table>

      <!--题目添加到题库表单-->
      <el-dialog title="添加题目" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="选择" :label-width="formLabelWidth">
            <div>
              <el-radio v-model="radio1" label="1" border>全部</el-radio>
              <el-radio v-model="radio1" label="2" border>指定题库</el-radio>
                <el-select v-model="searchObj.repoId" filterable placeholder="选择题库" v-if="this.radio1==='2'">
                  <el-option
                    v-for="item in option"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
            </div>
          </el-form-item>
        </el-form>
        <!--表单中搜索功能-->
          <el-form label-width="70px" size="small">
            <el-row>
              <el-col :span="6">
                  <el-select v-model="searchObj.quType" placeholder="全部类型">
                    <el-option
                      v-for="item in QuTypeOption"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                    </el-option>
                  </el-select>
              </el-col>
              <el-col :span="6">
                <el-input style="width: 95%" v-model="searchObj.content" placeholder="关键字"></el-input>
              </el-col>
              <el-col :span="12">
                <el-form-item label="操作时间">
                  <el-date-picker
                    v-model="createTimes"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    style="margin-right: 10px;width: 100%;"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row style="display:flex">
              <el-button type="primary" icon="el-icon-search" size="mini"  @click="fetchAllData()">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetAllData">重置</el-button>
            </el-row>
          </el-form>

        <el-table :data="listAll" height="250">
          <el-table-column  prop="ChineseTypeQu" label="题目类型" width="80"/>
          <el-table-column  prop="content" label="题目" show-overflow-tooltip/>
          <el-table-column prop="createTime" label="创建时间" width="180" />
          <el-table-column  label="操作"  align="center" fixed="right">
            <template slot-scope="scope">
              <el-button type="success" :disabled="$hasBP('bnt.assignment.update')  === false" icon="el-icon-plus" size="mini" @click="AddQuToRepo(scope.row.id)" title="添加"/>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">完成</el-button>
        </div>
      </el-dialog>

    </div>
  </template>
  <style>
      .search-div {
          padding:10px;border: 1px solid #EBEEF5;border-radius:3px;
      }
      .tools-div {
          margin-top: 10px;padding:10px;border: 1px solid #EBEEF5;border-radius:3px;
      }
  </style>
  <script>
  import api from '@/api/El/qu'
  import papi from '@/api/El/paper'
  const defaultForm = {
    id: '',
    url: '',
    content:'',
    createTime:'',
    updateTime:'',
    quType:'',
    optionsA:'',
    optionsB:'',
    optionsC:'',
    optionsD:'',
    answer:'',
  }
  export default {
    data() {
      return {
        radio1:'1',
        listLoading: true, // 数据是否正在加载
        list: null, // banner列表
        listAll:null,

        searchObj: { // 查询题库封装名称
          quType:'',
          repoId:'',
          content:'',
        },
  
        createTimes: [],
  
        checkAll: false, // 是否全选
  
        dialogFormVisible: false,
        ElQu: defaultForm,
        option: [{
          id: '',
          name: ''
        }],
        QuTypeOption:[
            {
                id: '1',
                name: '单选题'
            },
            {
                id: '2',
                name: '多选题'
            },
            {
                id: '3',
                name: '判断题'
            },
            {
                id: '4',
                name: '论述题'
            },
        ],

        QuThreeAnswer:[ //判断题答案
            {
                id: '1',
                name: '对'
            },
            {
                id: '2',
                name: '错'
            },
        ],


      }
    },
    // 生命周期函数：内存准备完毕，页面渲染成功
    mounted() {
      this.fetchData()
    },
  
    methods: {
      
      // 加载banner列表数据
      fetchData() {
        // 定义数字和题目的对应关系
        var numberMap = {
            "1": "单选题",
            "2": "多选题",
            "3": "判断题",
            "4": "论述题",
        };
        if(this.createTimes && this.createTimes.length ==2) {
            this.searchObj.createTimeBegin = this.createTimes[0]
            this.searchObj.createTimeEnd = this.createTimes[1]
        }
        api.GetRepoAndId().then(resp=>{
            this.option = resp.data
        })
        let paperid = this.$route.query.id
        
        papi.SelectQuByPaperId(paperid, this.searchObj).then(
            response => {
                this.list = response.data
                // 遍历数据，将quType替换成对应的题目类型
                this.list.forEach(element => {
                    element.ChineseTypeQu = numberMap[element.quType]
                    if(element.quType === "3"){
                      if(element.answer === "1"){
                        element.answer = "对"
                      }else{
                        element.answer = "错"
                      }
                    }
                });
                

                // 数据加载并绑定成功
                this.listLoading = false
            }
      )
    },
    //图片相关
    handleAvatarSuccess(res, file) {
        this.$message.success(res.message);
        this.ElQu.url = res.data.imgUrl;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      // 重置查询表单
      resetData() {
        this.option = []
        this.searchObj={}
        this.createTimes = []
        this.fetchData()
      },
      resetAllData(){
        this.searchObj={}
        this.createTimes = []
        this.radio1 = '1'
        this.fetchAllData()
      },
  
      // 根据id删除数据
      removeDataById(id){
            this.$confirm('此操作将从试卷中移除该题目, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              return papi.removeQuToPaper(this.$route.query.id,id)
            }).then((response) => {
              this.fetchData()
              this.$message.success(response.message || '移除')
            }).catch(() => {
              this.$message.info('取消')
            })
      },
  
      //弹出添加表单
      add(){
        this.ElQu = {}
        this.fetchAllData()
        this.dialogFormVisible = true
        },
        fetchAllData(){
          // 定义数字和题目的对应关系
          var numberMap = {
              "1": "单选题",
              "2": "多选题",
              "3": "判断题",
              "4": "论述题",
          };
          if(this.createTimes && this.createTimes.length ==2) {
            this.searchObj.createTimeBegin = this.createTimes[0]
            this.searchObj.createTimeEnd = this.createTimes[1]
          }
          api.getAll(this.searchObj).then(resp => {
            this.listAll = resp.data
            this.listAll.forEach(element => {
              element.ChineseTypeQu = numberMap[element.quType]
              if(element.quType === "3"){
                if(element.answer === "1"){
                  element.answer = "对"
                }else{
                  element.answer = "错"
                }
              }
          });
        })
      },

      //添加
      AddQuToRepo(id){
        papi.AddQuToRepo(this.$route.query.id,id).then(resp => {
          this.$message.success('添加成功!')
          this.fetchAllData()
          this.fetchData()
        })
      },

      },
      
    }
  </script>